<?php
include_once 'db.php';
session_start();
if(!$_SESSION["passed"]) return false;

$returnObj = array("code"=>"0", "desc" => "新增任务失败,缺少必填属性");

$actionType = $_POST["atype"];
$param = $_POST["param"];
// $param = json_decode($queryParam, true);
// echo $queryParam;
// $m_code = $_POST["mid"]; // mission_code任务主键
// $m_name = $_POST["missionName"]; // 任务名称
// $m_status = $_POST["missionStatus"]; // 任务状态
// $m_end_date = $_POST["endDate"]; // 结束日期
// $m_desc = $_POST["desc"]; // 任务简介
// $m_employee_num = $_POST["employerNum"]; // 待寻人员类别及数量
// $user_code = $_POST["userCode"]; // 任务创建人id



/**
 * [exist_mission 判断任务记录是否存在,删除或更新前应该先执行一次]
 * @param  [String]  $m_code [任务id]
 * @return [Boolean]         [true-存在;false-不存在]
 */
function exist_mission($m_code) {
    $count = 0;
    if(!$m_code) return false;
    $query_str = "select count(*) as count from l_mission where m_code =  %q";
    $query_param = array($m_code);
    $count = mysql_fetch_assoc(dbOperate($query_str, $query_param));
    $count = $count['count'];
    return $count > 0 ? true : false;
}
/**
 * [get_mission_num description]
 * @param  [String]  $query_where [查询where语句]
 * @param  [Object]  $query_param [查询条件参数]
 * @return [Integer]              [1-成功;0-失败]
 */
function get_mission_num($query_where, $query_param) {
    $query_str = "select count(*) as count from l_mission where 1 = 1 ".$query_where;
    // echo $query_str;
    $count = mysql_fetch_assoc(dbOperate($query_str, $query_param));
    $count = $count['count'];
    return $count ? $count : 0;
}
/**
 * [insert_mission  新增任务]
 * @param  [String]   $m_name         [任务名称]
 * @param  [integer]  $m_status       [任务状态]
 * @param  [String]   $m_end_date     [结束时间]
 * @param  [String]   $m_desc         [任务简介]
 * @param  [Integer]  $m_employee_num [所需人员]
 * @param  [String]   $user_code      [用户id]
 * @return [Integer]                  [1-成功;0-失败]
 */
function insert_mission($param) {
    $result = 0;
    $m_name = $param['m_name'];
    $m_status = $param['m_status'] ? $param['m_status'] : (NEED_VERIFY_STEP1 ? 0 : 1);
    $m_end_date = $param['m_end_date'];
    $m_desc = $param['m_desc'];
    $m_employee_num = $param['m_employee_num'];
    $m_employees = $param['m_employees'];
    $user_code = $param['user_code'];
    if(!$m_name || !$m_end_date || !$m_employee_num || !$user_code) return $result;

    $query_str = "insert into l_mission (id, m_code, m_name, m_status, m_end_date, m_desc,"
        ." m_employee_num, m_employees, user_code, last_modify) values ('', '".uniqid()."', %q, %i, %q, %n, %q, '', %q, now())";
    $query_param = array($m_name, $m_status, $m_end_date, $m_desc, $m_employee_num, $user_code);

    $result = dbOperate($query_str, $query_param);
    return $result;
}
/**
 * [delete_mission 删除任务]
 * @param  [String] $m_code [任务id]
 * @return [Int]            [mysql返回值,1:成功,0:失败]
 */
function delete_mission($param) {
    $result = 0;
    $m_code = $param['m_code'];
    if(!$m_code) return $result;
    $query_str = "delete from l_mission where m_code = %q";
    $query_param = array($m_code);

    $result = dbOperate($query_str, $query_param);
    return $result;
}
/**
 * [update_mission 更新任务]
 * @param  [String]  $m_name         [任务名称]
 * @param  [integer] $m_status       [任务状态]
 * @param  [String]  $m_end_date     [结束日期]
 * @param  [String]  $m_desc         [任务简介]
 * @param  [String]  $m_employee_num [所需人员,格式为(类型:数量,...,类型:数量)]
 * @param  [String]  $m_employees    [所选人员id字符串,','分隔]
 * @param  [String]  $user_code      [任务创建人]
 * @return [Int]                     [mysql返回值,1:成功,0:失败]
 */
function update_mission($param) {
    $result = 0;
    $m_name = $param['m_name'];
    $m_status = $param['m_status'] ? $param['m_status'] : (NEED_VERIFY_STEP2 ? 2 : 3);
    $m_end_date = $param['m_end_date'];
    $m_desc = $param['m_desc'];
    $m_employee_num = $param['m_employee_num'];
    $m_employees = $param['m_employees'];
    $user_code = $param['user_code'];
    $m_code = $param['m_code'];
    $clip = array();
    $query_param = array();
    // if(!$m_name || !$m_end_date || !$m_employee_num || !$user_code) return $result;

    if($m_name) {
        array_push($clip, 'm_name = %q, ');
        array_push($query_param, $m_name);
    }
    if($m_status) {
        array_push($clip, 'm_status = %i, ');
        array_push($query_param, $m_status);
    }
    if($m_end_date) {
        array_push($clip, 'm_end_date = %q, ');
        array_push($query_param, $m_end_date);
    }
    if($m_desc) {
        array_push($clip, 'm_desc = %q, ');
        array_push($query_param, $m_desc);
    }
    if($m_employee_num) {
        array_push($clip, 'm_employee_num = %q, ');
        array_push($query_param, $m_employee_num);
    }
    if($m_employees) {
        array_push($clip, 'm_employees = %q, ');
        array_push($query_param, $m_employees);
    }
    if($user_code) {
        array_push($clip, 'user_code = %q, ');
        array_push($query_param, $user_code);
    }
    array_push($query_param, $m_code);
    $query_str = "update l_mission set ".join('', $clip)."last_modify = now() where m_code = %q";
    // $query_str = "update l_mission set m_name = %q, m_status = %i, m_end_date = %q, m_desc = %q,"
    //     ." m_employee_num = %q, m_employees = %q, user_code = %q, last_modify = now() where m_code = %q";
    // $query_param = array($m_name, $m_status, $m_end_date, $m_desc, $m_employee_num, $m_employees, $user_code, $m_code);

    $result = dbOperate($query_str, $query_param);
    return $result;
}
/**
 * [query_mission 任务查询]
 * @param  [Integer] $user_type [用户类别, 0-雇员;1-雇主]
 * @param  [String]  $user_code [用户id]
 * @param  [String]  $user_name [用户名称]
 * @param  [Integer] $page      [当前页码]
 * @param  [Integer] $page_size [每页条数]
 * @param  [String]  $sortName  [排序字段]
 * @param  [String]  $sortOrder [排序类别,desc-降序;asc-升序]
 * @return [Object]             [任务记录列表{Rows: [], Total:0}]
 */
function query_mission($param) {
    $result = 0;

    // var_dump($param);
    // echo $param['user_code'].",".$param['user_name']."  ";
    $user_type = $param['userType'];
    $user_code = $param['userCode'];
    $user_name = $param['userName'];
    $page = $param['page'];
    $page_size = $param['pageSize'];
    $sortName = $param['sortName'] ? $param['sortName'] : 'last_modify';
    $sortOrder = $param['sortOrder'] ? $param['sortOrder'] : 'desc';

    $query_param = array();
    $clip = "";
    if(!$user_code || !$user_name) return $result;
    if(!!$user_code) {
        $clip = $clip." and user_code = %q ";
        array_push($query_param, $user_code);
    }
    if(!!$user_name && $user_type == 0) {
        $clip = $clip." and instr('m_employees', '%s') > -1 ";
        array_push($query_param, $user_name);
    }
    // echo $user_code.",".$user_name."  ";
    $count = get_mission_num($clip, $query_param);
    $pages = intval($count / $page_size);
    $pages = $count % $page_size > 0 ? $pages + 1 : $pages;

    array_push($query_param, $page - 1);
    array_push($query_param, $page_size);
    // var_dump($query_param);
    $query_str = "select l.*, (select s_desc from d_m_status where l.m_status = s_code) as status from l_mission l where 1 = 1 ".$clip." order by ".$sortName." ".$sortOrder." limit %i, %i";
    // echo $query_str;
    $result = dbOperate($query_str, $query_param);

    $arr = array();
    while($row = mysql_fetch_assoc($result))
    {
      array_push($arr, $row);
    }
    $rows = array("Rows"=>$arr, "curPage"=>$page, "pages"=>$pages);

    return json_encode($rows);
}

$result = 0;
switch($actionType) {
    case "c":
        $result = insert_mission($param);
        break;
    case "r":
        $result = query_mission($param);
        break;
    case "u":
        $result = update_mission($param);
        break;
    case "d":
        $result = delete_mission($param);
        break;
    default:
        $result = 0;
        break;
}
echo $result;

?>